// pages/my/wallet/wallet.js
const app = getApp();
var url = app.getURL();

Page({
  data: {
    money:'',
    totalMoney:0,
    modalName:null,
    checkbox: [{
      value: 10,
      name: '10元',
      checked: false,
      hot: false,
    }, {
      value: 30,
      name: '30元',
      checked: false,
      hot: false,
    }, {
      value: 50,
      name: '50元',
      checked: false,
      hot: true,
    }, {
      value: 100,
      name: '100元',
      checked: false,
      hot: true,
    }, {
      value: 200,
      name: '200元',
      checked: false,
      hot: false,
    }, {
      value: 500,
      name: '500元',
      checked: false,
      hot: false,
    }]
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null,

      // 将钱清空
      totalMoney:0
    })
  },
  ChooseCheckbox(e) {
    let items = this.data.checkbox;
    let values = e.currentTarget.dataset.value;
    // 计算总价
    // 是否选中，背景色亮灭
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      if (items[i].value == values) {
        items[i].checked = !items[i].checked;
        break
        }
      }
    this.setData({
      checkbox: items,
    })
  },
  onLoad(){
    
    // 查询余额
    this.getMoney();
  },

  getMoney(){
    let that = this
    let id = wx.getStorageSync('message').id;
    wx.request({
      url: url+'/account/getAccByOwnerId',
      method: "POST",
      header: {
        'content-type': 'application/json',
      },
      data:id,
      success(res) {
        if (res.data.code!='200') {
          console.log("未开通")
          that.openWallet()
        }else{
          that.setData({
            money:res.data.data.money
          })
        }
      }
    })
  },  
  // 开通钱包
  openWallet(){
    let id = wx.getStorageSync('message').id;
    wx.request({
      url: url+'/user/app/user/account/addAccount',
      method: "POST",
      header: {
        'content-type': 'application/json',
      },
      data:id,
      success(res) {
        console.log(res.data.message)
        wx.showToast({
          title: '钱包功能已成功开通',
          icon: 'success',
          duration: 800
        })
      }
    })
  }, 
  // 充值
  pay(e){
    let items = this.data.checkbox;
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
        if(items[i].checked == true){
          // console.log(items[i].value)
          this.setData({
            totalMoney: this.data.totalMoney + items[i].value
          })
      }
    }
    console.log(this.data.totalMoney)
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  ewmWay(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  cardWay(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  // 查询点卡
  getCard(){
    wx.request({
      url: url+'/user/app/cust/payCard/auth/getPayCard/'+this.data.totalMoney,
      data:{
        // payMoney: this.data.totalMoney + 9000
      },
      method: 'POST', 
      header: {// 设置请求的 header
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      success: function (res) {
        console.log(res.data)
      },
      fail: function (res) {
        console.log('错误' + ':' + res)
      }
    })
  },
  // 点卡充值提交
  formSubmit: function(e) {
    let that = this
    let id = wx.getStorageSync('message').id;
    console.log('form发生了submit事件，携带的数据为：', e.detail.value.cardCode)
    wx.request({
      url: url+'/user/app/cust/payCard/auth/usePayCard',
      data:{
        ownerId: id,
        payMoney: this.data.totalMoney,
        payCode: e.detail.value.cardCode
      },
      method: 'POST', 
      header: {// 设置请求的 header
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      success: function (res) {
        console.log(res.data)
        if(res.data.code==200){
          wx.showToast({
            title: '充值成功',
            icon: 'success',
            duration: 1000
          })
          that.setData({
            modalName: null
          })
          //改变钱包余额
         that.getMoney()
        }else if(res.data.code==309){
          wx.showToast({
            title: '充值卡已使用',
            icon: 'error',
            duration: 1000
          })
        }else if(res.data.code==306){
          wx.showToast({
            title: '卡号金额不匹配',
            icon: 'error',
            duration: 1000
          })
        }else if(res.data.code==308){
          wx.showToast({
            title: '此卡不存在',
            icon: 'error',
            duration: 1000
          })
        }else{
          wx.showToast({
            title: '充值失败',
            icon: 'error',
            duration: 1000
          })
        }
        
      },
      fail: function (res) {
        console.log('错误' + ':' + res)
      }
    })
  },
  // 提现
  OTA(){
    wx.showToast({
      title: '暂不支持~',
      icon: 'error'
    })
  }
})